package it.easycrm.pages;

import it.easycrm.data.dao.interfaces.CustomerDao;
import it.easycrm.data.dataobjects.Customer;

import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;


/**
 * 
 * @author Francesco De Simone in 29/mar/2011
 *
 */
public class CustomerPage extends BasePage {
	
	private  Form<Object> form;
	
	@SpringBean
	CustomerDao customerDao;
	public CustomerPage() {
		final WebMarkupContainer wmc = new WebMarkupContainer("container");
		this.form = new Form<Object>("form");
		
		//inserisco i campi della form
		RequiredTextField<String> cognome = new RequiredTextField<String>("cognome");
		RequiredTextField<String> nome = new RequiredTextField<String>("nome");
		RequiredTextField<String> piva = new RequiredTextField<String>("piva");
		RequiredTextField<String> citta = new RequiredTextField<String>("citta");
		RequiredTextField<String> phone = new RequiredTextField<String>("phone");
		RequiredTextField<String> birthdate = new RequiredTextField<String>("birthdate");
		form.add(cognome);
		form.add(nome);
		form.add(piva);
		form.add(citta);
		form.add(phone);
		form.add(birthdate);
		this.form.setOutputMarkupId(true);
		form.setVisible(false);
		wmc.add(form);
		wmc.add(new ListView<Customer>("list", new PropertyModel<List<Customer>>(this, "customerDao.findAll")){

			private static final long serialVersionUID = 1L;
			
			@Override
			protected void populateItem(ListItem<Customer> item) {
				Customer cus = item.getModelObject();
				item.add(new Label("id", String.valueOf(cus.getId())));
				item.add(new Label("cognome", cus.getCognome()));
				item.add(new Label("nome", cus.getNome()));
				item.add(new Label("citta",cus.getCity()));
				item.add(new Label("piva", cus.getPiva()));
			}
			
		});
		wmc.add(new AjaxLink<String>("add") {
			
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				
				form.setVisible(true);
				target.addComponent(wmc);
			}
		});
		FeedbackPanel feed = new FeedbackPanel("feed");
		feed.setOutputMarkupId(true);
		wmc.add(feed);
		wmc.setOutputMarkupId(true);
		add(wmc);
	}

}

